package com.lanchuan.soft.service.impl;

import com.lanchuan.soft.dao.IUserDao;
import com.lanchuan.soft.entity.ParamUser;
import com.lanchuan.soft.entity.User;
import com.lanchuan.soft.service.IUserService;
import com.lanchuan.soft.utils.CommonUtils;
import com.lanchuan.soft.utils.MD5Util;
import com.lanchuan.soft.utils.SystemConstant;
import lombok.extern.log4j.Log4j2;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Log4j2
public class UserService implements IUserService {

    private IUserDao userDao;

    @Autowired
    public UserService(IUserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public Map<String, Object> login(ParamUser paramUser) {

        Map<String, Object> resultMap = new HashMap<>();

        // 验证参数
        if (StringUtils.isBlank(paramUser.getUsername()) || StringUtils.isBlank(paramUser.getPwd())
                || !SystemConstant.USER_TYPE_LIST.contains(paramUser.getType())) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            // 用户登陆
            paramUser.setPwd(MD5Util.stringMD5(paramUser.getPwd()));
            paramUser.setState(SystemConstant.USER_STATE_NORMAL);
            User user = userDao.findByCondition(paramUser);
            if (user == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            resultMap.put("errcode", SystemConstant.SUCCESS);
            resultMap.put("data", user);
            log.info(JSONObject.fromObject(resultMap).toString());
            return resultMap;
        } catch (Exception e) {
            log.error("UserService / login", e);
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }

    @Override
    public Map<String, Object> createUser(ParamUser paramUser) {

        Map<String, Object> resultMap = new HashMap<>();

        // 验证参数
        if (StringUtils.isBlank(paramUser.getUsername()) || StringUtils.isBlank(paramUser.getPwd())
                || StringUtils.isBlank(paramUser.getNickname()) || StringUtils.isBlank(paramUser.getOperator())) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            // 当前用户
            ParamUser pu = new ParamUser();
            pu.setUserId(paramUser.getOperator());
            pu.setType(SystemConstant.USER_TYPE_PLATFORM);
            pu.setState(SystemConstant.USER_STATE_NORMAL);
            User operator = userDao.findByCondition(pu);
            if (operator == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            // 添加用户
            paramUser.setUserId(CommonUtils.createKeyId());
            paramUser.setOrganization(operator.getOrganization());
            paramUser.setPwd(MD5Util.stringMD5(paramUser.getPwd()));
            paramUser.setType(SystemConstant.USER_TYPE_WORKER);
            paramUser.setState(SystemConstant.USER_STATE_NORMAL);
            int createResult = userDao.create(paramUser);
            if (createResult == 0) {
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            resultMap.put("errcode", SystemConstant.SUCCESS);
            log.info(JSONObject.fromObject(resultMap).toString());
            return resultMap;
        } catch (Exception e) {
            log.error("UserService / createUser", e);
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }

    @Override
    public Map<String, Object> updateUser(ParamUser paramUser) {

        Map<String, Object> resultMap = new HashMap<>();

        // 验证参数
        if ((StringUtils.isBlank(paramUser.getPwd()) && StringUtils.isBlank(paramUser.getNickname()))
                || StringUtils.isBlank(paramUser.getUsername()) || StringUtils.isBlank(paramUser.getOldPwd())) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            // 当前用户
            ParamUser pu = new ParamUser();
            pu.setUsername(paramUser.getUsername());
            pu.setPwd(MD5Util.stringMD5(paramUser.getOldPwd()));
            pu.setState(SystemConstant.USER_STATE_NORMAL);
            User operator = userDao.findByCondition(pu);
            if (operator == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            // 更新用户信息
            ParamUser p = new ParamUser();
            p.setUsername(paramUser.getUsername());
            if (StringUtils.isNotBlank(paramUser.getPwd())) {
                p.setPwd(MD5Util.stringMD5(paramUser.getPwd()));
            } else {
                p.setPwd(operator.getPwd());
            }
            if (StringUtils.isNotBlank(paramUser.getNickname())) {
                p.setNickname(paramUser.getNickname());
            } else {
                p.setNickname(operator.getNickname());
            }
            int updateResult = userDao.updateByUsername(p);
            if (updateResult == 0) {
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            resultMap.put("errcode", SystemConstant.SUCCESS);
            log.info(JSONObject.fromObject(resultMap).toString());
            return resultMap;
        } catch (Exception e) {
            log.error("UserService / updateUser", e);
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }

    @Override
    public Map<String, Object> findUserList() {

        Map<String, Object> resultMap = new HashMap<>();

        try {
            ParamUser paramSelectUser = new ParamUser();
            List<User> userList = userDao.findListByCondition(paramSelectUser);
            resultMap.put("list", userList);

            resultMap.put("errcode", SystemConstant.SUCCESS);
            return resultMap;
        } catch (Exception e) {
            log.error("UserService / findUserList", e);
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }

    @Override
    public Map<String, Object> findMemberList(ParamUser paramUser) {

        Map<String, Object> resultMap = new HashMap<>();

        // 验证参数
        if (StringUtils.isBlank(paramUser.getOperator())) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            // 当前用户
            ParamUser pu = new ParamUser();
            pu.setUserId(paramUser.getOperator());
            pu.setState(SystemConstant.USER_STATE_NORMAL);
            User operator = userDao.findByCondition(pu);
            if (operator == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            // 工作人员列表
            ParamUser p = new ParamUser();
            p.setOrganization(operator.getOrganization());
            p.setType(SystemConstant.USER_TYPE_WORKER);
            p.setState(SystemConstant.USER_STATE_NORMAL);
            List<User> userList = userDao.findListByCondition(p);
            resultMap.put("list", userList);

            resultMap.put("errcode", SystemConstant.SUCCESS);
            return resultMap;
        } catch (Exception e) {
            log.error("UserService / findMemberList", e);
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }

    @Override
    public Map<String, Object> findDepartmentList(ParamUser paramUser) {

        Map<String, Object> resultMap = new HashMap<>();

        // 验证参数
        if (StringUtils.isBlank(paramUser.getOperator())) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            // 当前用户
            ParamUser pu = new ParamUser();
            pu.setUserId(paramUser.getOperator());
            pu.setState(SystemConstant.USER_STATE_NORMAL);
            User operator = userDao.findByCondition(pu);
            if (operator == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            // 有关部门列表
            ParamUser p = new ParamUser();
            p.setType(SystemConstant.USER_TYPE_DEPARTMENT);
            p.setState(SystemConstant.USER_STATE_NORMAL);
            List<User> userList = userDao.findListByCondition(p);
            resultMap.put("list", userList);

            resultMap.put("errcode", SystemConstant.SUCCESS);
            return resultMap;
        } catch (Exception e) {
            log.error("UserService / findDepartmentList", e);
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }
}
